package com.example.SearchZB;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;

import android.os.AsyncTask;

import android.location.Location;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.*;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.example.SeachZB.R;
import org.json.JSONException;


import java.io.IOException;


import java.util.ArrayList;

import java.util.List;
import java.util.Map;

public class MainActivity extends Activity{

    private ListView serveList;

    private List<Map<String,Object>> serveData;
    private int id;



    private List<ServeData> list;
    private Button btn_setting,btn_search,btn_location;
    private LocationApplication locationApplication;
    public TextView locationText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);




        locationText = (TextView) findViewById(R.id.locationText);
        locationApplication = new LocationApplication(MainActivity.this,locationText);
        locationApplication.getCurrentLocation();

        list = new ArrayList<ServeData>();


        serveList = (ListView) findViewById(R.id.serveList);
        getMainData(id);

        btn_search = (Button) findViewById(R.id.btn_search);
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SearchActivity.class);
                startActivity(intent);
            }
        });

        btn_setting = (Button) findViewById(R.id.btn_setting);
        btn_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SettingActivity.class);
                startActivity(intent);
            }
        });

        btn_location = (Button) findViewById(R.id.btn_location);
        btn_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                locationApplication.getCurrentLocation();
            }


        });
    }


    public class mainAdapter extends BaseAdapter{



  


        @Override
        public int getCount() {
            return serveData.size();
        }

        @Override
        public Object getItem(int position) {
            return serveData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            final View view;
            if (convertView == null) {
                view = getLayoutInflater().inflate(R.layout.servelist_item, parent, false);
            } else {
                view = convertView;
            }
            TextView serveListText = (TextView) view.findViewById(R.id.serveListText);
            serveListText.setText((String) serveData.get(position).get("name"));

//            servelist_item.setText((String) serveData.get(position).get("id"));

            final String id = ((String) serveData.get(position).get("id"));
            final String name = ((String) serveData.get(position).get("name"));

            Button btn_nextstep = (Button) view.findViewById(R.id.btn_nextstep);
            btn_nextstep.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this,Lv2ListActivity.class);
                    intent.putExtra("id",id);
                    intent.putExtra("name",name);

                    startActivity(intent);
                }
            });

            return view;
        }
    }
    public void getMainData(final int n) {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("加载中…");

        AsyncTask<Integer, Integer, Integer> asyncTask = new AsyncTask<Integer, Integer, Integer>() {
            int error = 0;

            @Override
            protected Integer doInBackground(Integer... params) {
                CategoryManager categoryManager = new CategoryManager();
                try {
                    serveData = categoryManager.getCategoryData(n);
                } catch (IOException e) {
                    Log.e("", e.getMessage());
                    error = 1;
                    return error;
                } catch (JSONException e) {
                    error = 2;
                    return error;
                }
                return 0;
            }

            @Override
            protected void onPreExecute() {
                progressDialog.show();
                progressDialog.setMessage("加载中…");
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(Integer integer) {
                if (integer == 1) {
                    Toast.makeText(MainActivity.this, "网络出错，请稍候再试", Toast.LENGTH_SHORT).show();
                } else if (integer == 2) {
                    Toast.makeText(MainActivity.this, "服务器出错，请稍候再试", Toast.LENGTH_SHORT).show();
                } else {
                    mainAdapter mainAdapter = new mainAdapter();
                    serveList.setAdapter(mainAdapter);
                    progressDialog.dismiss();
                }
            }
            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
            }
        };
        asyncTask.execute();
    }
}
